package com.lucas.packdenp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lucas.packdenp.mapper.ArtifactMapper;
import com.lucas.packdenp.mapper.DepMapper;
import com.lucas.packdenp.pojo.entity.Artifact;
import com.lucas.packdenp.pojo.entity.ArtifactVo;
import com.lucas.packdenp.pojo.entity.Dep;
import com.lucas.packdenp.service.ArtifactService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@AllArgsConstructor
@Slf4j
public class ArtifactServiceImpl implements ArtifactService {

    @Resource
    private ArtifactMapper artifactMapper;

    @Resource
    private DepMapper depMapper;

    @Override
    public int addArt(Artifact artifact) {
        //字符串去空，前后的空字符串
        artifact.setArtifactid(artifact.getArtifactid().trim());
        if (checkBlank(artifact.getArtifactid())){
            return 0;
        }
        artifact.setGroupid(artifact.getGroupid().trim());
        if (checkBlank(artifact.getGroupid())){
            return 0;
        }
        artifact.setVersion(artifact.getVersion().trim());
        if (checkBlank(artifact.getVersion())){
            return 0;
        }
        int count = artifactMapper.selectExists(artifact);
        log.info("查询的数量:{}",count);
        if(count >0){
            return 0;
        }
        return artifactMapper.insert(artifact);
    }

    @Override
    public int delArt(Artifact artifact) {

        return 0;
    }

    @Override
    public int addDep(ArtifactVo vo) {
        vo.setArtifactid(vo.getArtifactid().trim());
        if (checkBlank(vo.getArtifactid())){
            return 0;
        }
        Dep dep = vo.getDep();
        dep.setArtifactid(dep.getArtifactid().trim());
        if (checkBlank(dep.getArtifactid())){
            return 0;
        }
        dep.setGroupid(dep.getGroupid().trim());
        if (checkBlank(dep.getGroupid())){
            return 0;
        }
        dep.setVersion(dep.getVersion().trim());
        if (checkBlank(dep.getVersion())){
            return 0;
        }
        QueryWrapper<Artifact> wrapper=new QueryWrapper<>();
        wrapper.eq("artifactid",vo.getArtifactid());
        Artifact artifact = artifactMapper.selectOne(wrapper);
        if (artifact==null || artifact.getId()==null){
            return 0;
        }
        String father=artifact.getId();
        dep.setFather(father);
        QueryWrapper<Dep> wrapper1=new QueryWrapper<>();
        wrapper1.eq("artifactid",dep.getArtifactid());
        wrapper1.eq("groupid",dep.getGroupid());
        wrapper1.eq("version",dep.getVersion());
        wrapper1.eq("father",father);
        if (depMapper.selectCount(wrapper1)>0){
            return 0;
        }
        depMapper.insert(dep);
        return 1;
    }

    private boolean checkBlank(String messgae){
        if (messgae==null){
            return true;
        }
        messgae=messgae.trim();
        if (messgae.isEmpty()){
            return true;
        }
        return false;
    }
}
